import okex.v5.account_api as account
import time
from okex.v5.ccytype import CcyType
import matplotlib.pyplot as plt


# 可视化账户持仓情况
def parse_and_plot_asset(asset_result):
    asset_dict = asset_result[0]
    if asset_dict['adjEq']:
        print("美金层面有效保证金,适用于跨币种保证金模式和组合保证金模式:", asset_dict['adjEq'])
    if asset_dict['imr']:
        print("美金层面占用保证金,适用于跨币种保证金模式和组合保证金模式", asset_dict['imr'])
    if asset_dict['isoEq']:
        print("美金层面逐仓仓位权益,适用于单币种保证金模式和跨币种保证金模式和组合保证金模式", asset_dict['isoEq'])
    if asset_dict['mgnRatio']:
        print("美金层面保证金率,适用于跨币种保证金模式 和组合保证金模式:", asset_dict['mgnRatio'])
    if asset_dict['mmr']:
        print("美金层面维持保证金,适用于跨币种保证金模式和组合保证金模式:", asset_dict['mmr'])
    if asset_dict['notionalUsd']:
        print("以美金价值为单位的持仓数量,即仓位美金价值,适用于跨币种保证金模式和组合保证金模式", asset_dict['notionalUsd'])
    if asset_dict['ordFroz']:
        print("美金层面全仓挂单占用保证金,适用于跨币种保证金模式和组合保证金模式", asset_dict['ordFroz'])
    if asset_dict['totalEq']:
        print("美金层面权益:", asset_dict['totalEq'])
    timestamp = int(asset_dict['uTime'])
    time_local = time.localtime(timestamp / 1000)
    otherStyleTime = time.strftime('%Y-%m-%d %H:%M:%S', time_local)
    print("账户信息的更新时间:", otherStyleTime)

    # 币种
    carried_coin_list = [asset['ccy'] for asset in asset_dict['details']]
    print(asset_dict['details'])
    # 币种总权益
    carried_coin_eq = [float(asset['eq']) for asset in asset_dict['details']]

    # 币种余额
    carried_coin_cashBal = [float(asset['cashBal']) for asset in asset_dict['details']]

    # 币种权益美金价值
    carried_coin_eqUsd = [float(asset['eqUsd']) for asset in asset_dict['details']]

    # 可用余额
    carried_coin_availBal = [float(asset['availBal']) for asset in asset_dict['details']]

    # 可用保证金
    carried_coin_availEq = [asset['availEq'] for asset in asset_dict['details']]

    # 美金层面币种折算收益
    carried_coin_disEq = [asset['disEq'] for asset in asset_dict['details']]

    # 策略权益
    carried_coin_stgyEq = [asset['stgyEq'] for asset in asset_dict['details']]

    # 币种余额更新时间
    # 币种逐仓仓位权益
    # 币种占用金额
    # 挂单冻结数量
    # 币种负债额
    # 未实现盈亏
    # 由于仓位未实现亏损导致的负债
    # 币种全仓负债额
    # 币种逐仓负债额
    # 保证金率
    # 计息
    # 当前负债币种触发交易系统自动换币的风险
    # 币种最大可借
    # 币种杠杆倍数
    # 逐仓未实现盈亏

    # 画图
    ## 美金价值
    plt.figure(1)
    plt.title("持有币等值美金权益")
    plt.bar(carried_coin_list, carried_coin_eqUsd, color='rgb')

    ## 可用币余额
    plt.figure(2)
    plt.title("可用币余额")
    plt.bar(carried_coin_list, carried_coin_cashBal, color='rgb')
    for i, coin in enumerate(carried_coin_list):
        print("可用{}余额为:{}".format(coin, carried_coin_cashBal[i]))

    # plt.figure(3)
    # plt.bar(carried_coin_list,carried_coin_eq,color = 'rgb')

    plt.show()


# 获取币种余额信息
def accquire_exact_coin_balance(api_key, secret_key, passphrase, coin_name, test=True):
    # 资金账户API
    accountAPI = account.AccountAPI(api_key, secret_key, passphrase, False, test=test)
    account_left = []
    ## 查看账户余额
    while account_left == None:
        try:
            account_left = accountAPI.balance(ccyType=CcyType.USDT)
        except:
            time.sleep(5)
    account_left_keys = [coin_dict['ccy'] for coin_dict in account_left[0]['details']]
    account_left_values = account_left[0]['details']
    accout_left_dict = dict(zip(account_left_keys, account_left_values))

    return float(accout_left_dict[coin_name]['availBal'])
